package com.google.shop.product.controller.card;

import com.google.shop.product.common.to.CardTO;
import com.google.shop.product.common.utils.JwtUtils;
import com.google.shop.product.common.utils.R;
import com.google.shop.product.entity.card.Card;
import com.google.shop.product.repository.card.CardRepository;
import com.google.shop.product.service.card.CardService;
import com.google.shop.product.vo.card.CardVO;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping({"/card"})
@RestController
/* loaded from: app.jar:BOOT-INF/classes/com/google/shop/product/controller/card/CardController.class */
public class CardController {
    private static final Logger log = LoggerFactory.getLogger(CardController.class);
    @Autowired
    CardService cardService;
    @Autowired
    CardRepository cardRepository;

    @GetMapping({"/list"})
    public R list(String token) {
        if (token == null) {
            return R.error("token为空");
        }
        return R.ok(this.cardService.list(JwtUtils.getMemberIdByJwtToken(token)));
    }

    @GetMapping({"/updateCardIsOpt"})
    public R updateCardIsOpt(String token, Integer skuId, Integer isOpt) {
        if (token == null) {
            return R.error("token为空");
        }
        this.cardService.updateCardIsOpt(JwtUtils.getMemberIdByJwtToken(token), skuId, isOpt);
        return R.ok();
    }

    @GetMapping({"/updateCardSkuNum"})
    public R updateCardSkuNum(String token, Integer skuId, Integer skuNum) {
        if (token == null) {
            return R.error("token为空");
        }
        this.cardService.updateCardSkuNum(JwtUtils.getMemberIdByJwtToken(token), skuId, skuNum);
        return R.ok();
    }

    @GetMapping({"/updateAllCardOpt"})
    public R updateAllCardOpt(String token, Boolean isAllOpt) {
        if (token == null) {
            return R.error("token为空");
        }
        this.cardService.updateAllCardOpt(JwtUtils.getMemberIdByJwtToken(token), isAllOpt);
        return R.ok();
    }

    @PostMapping({"/saveItemGoCard"})
    public R saveItemGoCard(@RequestBody CardVO cardVO) {
        if (cardVO.getToken() == null) {
            return R.error("token为空");
        }
        this.cardService.saveItemGoCard(JwtUtils.getMemberIdByJwtToken(cardVO.getToken()), cardVO);
        return R.ok();
    }

    @GetMapping({"/deleteCard"})
    public R deleteCard(String token) {
        if (token == null) {
            return R.error("token为空");
        }
        this.cardService.deleteCard(JwtUtils.getMemberIdByJwtToken(token));
        return R.ok();
    }

    @GetMapping({"/findCardDetailByBuyerId"})
    public R findCardDetailByBuyerId(String token) {
        if (token == null) {
            return R.error("token为空");
        }
        return R.ok(this.cardService.findCardDetailByBuyerId(JwtUtils.getMemberIdByJwtToken(token)));
    }

    @GetMapping({"/feignFindCardListByOkOpt/{token}"})
    public List<CardTO> feignFindCardDetailByBuyerId(@PathVariable String token) {
        if (token == null) {
            return null;
        }
        List<Card> cardList = this.cardRepository.findCardsByBuyerIdAndAndCardSkuIsopt(Integer.valueOf(JwtUtils.getMemberIdByJwtToken(token)), 0);
        List<CardTO> cardTOList = new ArrayList<>();
        for (Card card : cardList) {
            CardTO cardTO = new CardTO();
            BeanUtils.copyProperties(card, cardTO);
            cardTOList.add(cardTO);
        }
        return cardTOList;
    }

    @GetMapping({"/feignDeleteCardListByOkOpt/{token}"})
    public R feignDeleteCardListByOkOpt(@PathVariable String token) {
        if (token == null) {
            return null;
        }
        this.cardRepository.deleteByBuyerIdAndCardSkuIsopt(Integer.valueOf(JwtUtils.getMemberIdByJwtToken(token)), 0);
        return R.ok();
    }
}
